#include "Interface.h"

#include "iostream"
#include "iomanip"
using namespace std;

void displayMenu(DonorList &donorList)
{
    string yesOrNo = "y";
    do {
        cout
            << "******************************"
               "*****************************"
            << endl
            << "                         MAIN "
               "MENU                         "
            << endl
            << "******************************"
               "*****************************"
            << endl << endl;
        cout << "Select one of the following: " << endl << endl
             << "    1. Add a donor" << endl
             << "    2. Delete a donor" << endl
             << "    3. Print all donors" << endl
             << "    4. Print all donations" << endl
             << "    5. Print total donations" << endl
             << "    6. Print highest donation" << endl
             << "    7: To exit" << endl << endl
             << "  => Enter your selection: ";
        int selection = -1;
        cin >> selection;
        cout << endl;
        if (selection != 7)
        {
            processSelection(selection, donorList);
            cout << endl << "  => Would you like to continue? (y/n) ";
            cin >> yesOrNo;
            cout << endl;
        }
        else
        {
            break;
        }
    } while (yesOrNo == "y");
    cout << "  => Thank you for visiting our site!" << endl;
}

void processSelection(const int &selection, DonorList &donorList)
{

    if (selection == 1)
    {
        addDonor(donorList);
    }
    else if (selection == 2)
    {
        deleteDonor(donorList);
    }
    else if (selection == 3)
    {
        printAllDonors(donorList);
    }
    else if (selection == 4)
    {
        printAllDonations(donorList);
    }
    else if (selection == 5)
    {
        printTotalDonations(donorList);
    }
    else if (selection == 6)
    {
        printHighestDonation(donorList);
    }
    else
    {
        cout << "  => Sorry. That is not a selection." << endl;
    }
}

void addDonor(DonorList &donorList)
{
    int number = -1;
    string firstName = "";
    string lastName = "";
    double donated = -1;

    cout << "  => Enter donor's first name: ";
    cin >> firstName;
    cout << "  => Enter donor's last name: ";
    cin >> lastName;
    cout << "  => Enter donor's membership number: ";
    cin >> number;
    cout << "  => Enter donor's donated: $";
    cin >> donated;

    donorList.addDonor(firstName, lastName, number, donated);
    cout << endl << "  => Donor has been added." << endl;
}

void deleteDonor(DonorList &donorList)
{
    if (donorList.isEmpty())
    {
        cout << "The database has no donors." << endl;
    }
    else
    {
        cout << "  => Enter donor's membership number: ";
        int number = -1;
        cin >> number;
        bool searchResult = donorList.searchID(number);
        if (searchResult)
        {
            donorList.deleteDonor(number);
        }
        cout << endl << "  => Donor has been deleted." << endl;
    }
}

void printAllDonors(const DonorList &donorList)
{
    donorList.printAllDonors();
}

void printAllDonations(const DonorList &donorList)
{
    donorList.printAllDonations();
}

void printTotalDonations(const DonorList &donorList)
{
    cout << setiosflags(ios::fixed);
    cout << "     Total donations: $" << setprecision(2)
        << donorList.getTotalDonations() << endl;
}

void printHighestDonation(const DonorList &donorList)
{
    cout << setiosflags(ios::fixed);
    cout << "     Highest donation: $" << setprecision(2)
        << donorList.getHighestDonation() << endl;
}
